/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */

var tab;

layui.config({
    base: basePath+'/static/common/layui/js/',
    version: new Date().getTime()
}).use(['element', 'layer', 'navbar', 'tab','newAjax'], function () {
    var element = layui.element(),
        $ = layui.jquery,
        layer = layui.layer,
        navbar = layui.navbar(),
        ajax=layui.newAjax;
    tab = layui.tab({
        elem: '.admin-nav-card' //设置选项卡容器
        ,autoRefresh:true,
        //maxSetting: {
        //	max: 5,
        //	tipMsg: '只能开5个哇，不能再开了。真的。'
        //},
        contextMenu: true,
        onSwitch: function (data) {
            // console.log(data.id); //当前Tab的Id
            // console.log(data.index); //得到当前Tab的所在下标
            // console.log(data.elem); //得到当前的Tab大容器

            // console.log(tab.getCurrentTabId())
        },
        closeBefore: function (obj) { //tab 关闭之前触发的事件
            // console.log(obj);
            //obj.title  -- 标题
            //obj.url    -- 链接地址
            //obj.id     -- id
            //obj.tabId  -- lay-id
            if (obj.title === 'BTable') {
                layer.confirm('确定要关闭' + obj.title + '吗?', { icon: 3, title: '系统提示' }, function (index) {
                    //因为confirm是非阻塞的，所以这里关闭当前tab需要调用一下deleteTab方法
                    tab.deleteTab(obj.tabId);
                    layer.close(index);
                });
                //返回true会直接关闭当前tab
                return false;
            }else if(obj.title==='表单'){
                layer.confirm('未保存的数据可能会丢失哦，确定要关闭吗?', { icon: 3, title: '系统提示' }, function (index) {
                    tab.deleteTab(obj.tabId);
                    layer.close(index);
                });
                return false;
            }
            return true;
        },
        /** 跳转到制定title的页面 */
        jumpTo:function (title, paramUrl) {
            var that=this;
            var t_tab;
            var datas = navbar.config.data;
            for(var i in datas){
                if(title===datas[i].title){
                    t_tab=datas[i];
                    break;
                }
                for(var j in datas[i].children || []){
                    if(title===datas[i].children[j].title){
                        t_tab=datas[i].children[j];
                        break;
                    }
                }
            }
            if(paramUrl){
                t_tab.refreshHref = t_tab.href.trim()+paramUrl;
            }else {
                t_tab.refreshHref=t_tab.href;
            }
            tab.tabAdd(t_tab);
        }
    });
    //iframe自适应
    $(window).on('resize', function () {
        var $content = $('.admin-nav-card .layui-tab-content');
        $content.height($(this).height() - 147);
        $content.find('iframe').each(function () {
            $(this).height($content.height());
        });
    }).resize();

    //设置navbar
    navbar.set({
        spreadOne: true,
        elem: '#admin-navbar-side',
        cached: true,
        data: navs,
		//cached:true,
		url: 'plat/user/menu.html'
    });
    //渲染navbar
    navbar.render();
    //监听点击事件
    navbar.on('click(side)', function (data) {
        tab.tabAdd(data.field);
    });

    // 侧边栏隐藏显示
    $('.admin-side-toggle').on('click', function () {
        var sideWidth = $('#admin-side').width();
        if (sideWidth === 160) {
            $('#admin-body').animate({
                left: '0'
            }); //admin-footer
            $('#admin-footer').animate({
                left: '0'
            });
            $('#admin-side').animate({
                width: '0'
            });
        } else {
            $('#admin-body').animate({
                left: '160px'
            });
            $('#admin-footer').animate({
                left: '160px'
            });
            $('#admin-side').animate({
                width: '160px'
            });
        }
    });

    // 全屏
    $('.admin-side-full').on('click', function () {
        var docElm = document.documentElement;
        //W3C  
        if (docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }
        //FireFox  
        else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }
        //Chrome等  
        else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
        //IE11
        else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
        layer.msg('按Esc即可退出全屏');
    });

    //个人设置
    $('#setting').on('click', function () {
        tab.tabAdd({
            href: '/Manage/Account/Setting/',
            icon: 'fa-gear',
            title: '设置'
        });
    });

    //锁屏
    $(document).on('keydown', function () {
        var e = window.event;
        if (e.keyCode === 76 && e.altKey) {
            //alert("你按下了alt+l");
            lock($, layer);
        }
    });
    $('#lock').on('click', function () {
        lock($, layer);
    });

    //手机设备的简单适配
    var treeMobile = $('.site-Tree-mobile'),
        shadeMobile = $('.site-mobile-shade');
    treeMobile.on('click', function () {
        $('body').addClass('site-mobile');
    });
    shadeMobile.on('click', function () {
        $('body').removeClass('site-mobile');
    });

    var isShowLock = false;
    function lock($, layer) {
        if (isShowLock)
            return;
        //自定页
        layer.open({
            title: false,
            type: 1,
            closeBtn: 0,
            anim: 6,
            content: $('#lock-temp').html(),
            shade: [0.9, '#393D49'],
            success: function (layero, lockIndex) {
                isShowLock = true;

                $.getJSON(basePath+'plat/lockScreen.html');

                //绑定解锁按钮的点击事件
                layero.find('button#unlock').on('click', function () {
                    var $lockBox = $('div#lock-box');

                    var userName = $lockBox.find('input[name=username]').val();
                    var pwd = $lockBox.find('input[name=lockPwd]').val();
                    if (!pwd || pwd === '输入密码解锁..' || pwd.length === 0) {
                        layer.msg('请输入密码..', {
                            icon: 2,
                            time: 1000
                        });
                        return;
                    }
                    unlock(userName, pwd);
                });
                /**
                 * 解锁操作方法
                 * @param {String} 用户名
                 * @param {String} 密码
                 */
                var unlock = function (un, pwd) {
                    //这里可以使用ajax方法解锁
                    ajax.post(basePath+'unlockScreen.html',{ username: un, password: hex_md5(pwd) },
                        function (data) {
                            layer.msg(data);
                            //关闭锁屏层
                            layer.close(lockIndex);
                            isShowLock = false;
                    })
                };
            }
        });
    };



});
